package com.hitgo.soccer.handler;

import com.hitgo.soccer.core.Match;
import com.hitgo.soccer.core.MatchData;
import com.hitgo.soccer.core.Player;
import com.hitgo.soccer.core.Team;
import com.hitgo.soccer.database.MatchDAO;
import com.hitgo.soccer.database.PlayerDAO;
import com.hitgo.soccer.database.TeamDAO;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MatchHandler extends BaseHandler
{
	private static BaseHandler instance = null;
	
	public static BaseHandler getInstance()
	{
		if(instance == null)
			instance = new MatchHandler();
		return instance;
	}
	
	public void cmdHandler(int nCmd, HttpServletRequest req, HttpServletResponse res)
	{
		String strParams = req.getParameter("fbParams");
		switch(nCmd)
		{
		case Commands.MATCH_START_FRIENDSHIP:
			this.startFrindshipMatch(res, strParams);
			break;
		case Commands.MATCH_START_LEAGUE:
			this.startArenaMatch(res, strParams);
			break;
		case Commands.MATCH_START_ARENA:
			this.startLeagueMatch(res, strParams);
			break;
		default:
			break;
		}
	}
	
	private void startFrindshipMatch(HttpServletResponse res, String strParams)
	{
		//TODO 要判断友谊赛剩余次数
		this.startMatch(res, strParams, Match.MATCH_TYPE_FRIENDSHIP);
	}
	
	private void startArenaMatch(HttpServletResponse res, String strParams)
	{
		//TODO 要判断竞技场剩余次数
		this.startMatch(res, strParams, Match.MATCH_TYPE_ARENA);
	}
	
	private void startLeagueMatch(HttpServletResponse res, String strParams)
	{
		//TODO 要判断联赛剩余次数
		this.startMatch(res, strParams, Match.MATCH_TYPE_LEAGUE);
	}

	private void startMatch(HttpServletResponse res, String strParams, int nMatchType)
	{
		strParams = strParams.trim();
		String[] params = strParams.split(Commands.PARAMS_SEPARATOR);
		Long lUserID = Long.parseLong(params[0]);
		Long lMatchID = Long.parseLong(params[1]);
		byte[] matchData = startMatch(lUserID, lMatchID);

		res.setContentType("text/plain");
		try
		{
			ServletOutputStream out = res.getOutputStream();
			if(matchData == null)
				out.write(0);
			else
				out.write(matchData);
			
			// 保存比赛数据
			MatchData md = new MatchData();
			
			md.setData(matchData);
			md.setType(nMatchType);
			md.setUser1ID(lUserID);
			md.setUser2ID(lMatchID);
			
			MatchDAO.insertMatchData(md);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
	}

	public byte[] startMatch(Long userID, Long matchID)
	{
		Team team1 = TeamDAO.getTeam(userID);
		if(team1 == null)
			return null;
		Player[] players = PlayerDAO.getPlayers(userID);
		if(players == null)
			return null;
		team1.setPlayers(players);
		
		Team team2 = TeamDAO.getTeam(matchID);
		if(team2 == null)
			return null;
		
		players = PlayerDAO.getPlayers(matchID);
		if(players == null)
			return null;
		team2.setPlayers(players);
		Match match = new Match(team1, team2);
		byte[] matchData = match.startMatch();
		
		return matchData;
	}
}